package best.driver.android.Map.location;

public class BestLocationHelper {
    public final static int LOCATION_NEW = 0;//新定位数据

    public final static int LOCATION_NEW_NODE = 1;//新定位数据,节点

    public final static int LOCATION_NO_NEW = 2;//无新定位数据

    public final static int LOCATION_INVALID_FIRST = 3;//无效数据，第一个定位点

    public final static int LOCATION_INVALID_DATA = 4;//无效数据

    public final static int LOCATION_INVALID_ACCURACY = 5;//位置低精确度

    public final static int LOCATION_INVALID_TOO_FAR = 6;//速度过快

    public final static int LOCATION_INVALID_GPS_SPEED = 7;//GPS速度无效,解决定点漂移

    public final static int LOCATION_INVALID_TIME_ERROR = 8;//定位点时序错误(后来点比之前点时间小)

    public final static int LOCATION_INVALID_DEGREE = 9;//角度算法抛掉的点

    public final static double M_PI_180 = 0.01745329252; // PI/180.0

    public final static double EARTH_RADIUS = 6370693.5; // radius of

    public final static double MIN_DISTANCE = 0.1f;

    public final static double MIN_GPS_SPEED = 1.0f;

    public final static double MIN_WAIT_DISTANCE = 1.0f;//开车时开启等候距离阈值

    public static String locationResultValue(int intValue) {
        String value = "LOCATION_NEW";
        switch (intValue) {
            case LOCATION_NEW_NODE:
                value = "LOCATION_NEW_NODE";
                break;
            case LOCATION_NO_NEW:
                value = "LOCATION_NO_NEW";
                break;
            case LOCATION_INVALID_FIRST:
                value = "LOCATION_INVALID_FIRST";
                break;
            case LOCATION_INVALID_DATA:
                value = "LOCATION_INVALID_DATA";
                break;
            case LOCATION_INVALID_ACCURACY:
                value = "LOCATION_INVALID_ACCURACY";
                break;
            case LOCATION_INVALID_TOO_FAR:
                value = "LOCATION_INVALID_TOO_FAR";
                break;
            case LOCATION_INVALID_GPS_SPEED:
                value = "LOCATION_INVALID_GPS_SPEED";
                break;
            case LOCATION_INVALID_TIME_ERROR:
                value = "LOC_INVALID_TIME_ERROR";
                break;
            case LOCATION_INVALID_DEGREE:
                value = "LOC_INVALID_DEGREE";
        }

        return value;
    }

    public static double getLongDistance(double lon1, double lat1, double lon2, double lat2) {
        double ew1, ns1, ew2, ns2;
        double distance;
        // 角度转换为弧度
        ew1 = lon1 * M_PI_180;
        ns1 = lat1 * M_PI_180;
        ew2 = lon2 * M_PI_180;
        ns2 = lat2 * M_PI_180;
        // 求大圆劣弧与球心所夹的角(弧度)
        distance = Math.sin(ns1) * Math.sin(ns2) + Math.cos(ns1) * Math.cos(ns2) * Math.cos(ew1 - ew2);
        // 调整到[-1..1]范围内，避免溢出
        if (distance > 1.0) {
            distance = 1.0;
        } else if (distance < -1.0) {
            distance = -1.0;
        }
        // 求大圆劣弧长度
        distance = EARTH_RADIUS * Math.acos(distance);
        return distance;
    }

    public static boolean isSameLocation(double lng1, double lat1, double lng2, double lat2) {
        if(lat1 > 1e-6 && lng1 > 1e-6) {
            if(Math.abs(lat1 - lat2) / lat1 < 1e-10 && Math.abs(lng1 - lng2) / lat2 < 1e-10) {
                return true;
            }

            if(BestLocationHelper.getLongDistance(lng1, lat1, lng2, lat2) < 5.0) {
                return true;
            }
            return false;
        }
        return false;
    }



}
